#ifndef FK_OBJECT_FACTORY
#define FK_OBJECT_FACTORY

#include <QMap>

template <class D>
class FKAbstractObjectCreator{
public:
    FKAbstractObjectCreator(){}
    virtual ~FKAbstractObjectCreator(){}
    virtual D* create()=0;
};

template <class E, class D>
class FKObjectCreator:public FKAbstractObjectCreator<D>{
public:
    FKObjectCreator(){}
    ~FKObjectCreator(){}
    D* create(){return new E;}
};

template <class D>
class FKObjectFactory{
public:
    FKObjectFactory(){}
    ~FKObjectFactory(){
        if(!_creators.isEmpty()){
            foreach(FKAbstractObjectCreator<D>* creator,_creators)delete creator;
        }
    }
    template <class E> void addClass(){
        QString className=E::staticMetaObject.className();
        if(_creators.contains(className))return;
        FKAbstractObjectCreator<D>* creator=new FKObjectCreator<E,D>;
        _creators.insert(className,creator);
    }
    D* create(QString className){
        if(!_creators.contains(className))return 0;
        return _creators.value(className)->create();
    }
private:
    QMap<QString,FKAbstractObjectCreator<D>* > _creators;
};

#endif
